package com.iss.demo.controller;

import com.iss.demo.entity.User;
import com.iss.demo.model.MyResponseData;
import com.iss.demo.model.UserWithRole;
import com.iss.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("user/")
public class UserController {
    @Autowired
    private UserService service;

    @RequestMapping("selectAll")
    public List<User> selectAll(HttpServletRequest request){
        List<User> list = service.selectAll();
        return list;
    }

    @RequestMapping("login")
    public MyResponseData login(User user, HttpSession session){
        MyResponseData responseData = new MyResponseData();
        UserWithRole loginUser = service.login(user);
        session.setAttribute("loginUser",loginUser);
        if(loginUser==null){
            responseData.setErrorCode(-1);
            responseData.setStatus("用户名和密码不正确");
        }
        else{
            if(loginUser.getR_id()==1)
            {   responseData.setErrorCode(1);
                responseData.setStatus("登录成功");
                responseData.setObject(loginUser);
            }else if(loginUser.getR_id()==2)
            {
                responseData.setErrorCode(2);
                responseData.setStatus("登录成功");
                responseData.setObject(loginUser);
            }else if(loginUser.getR_id()==3)
            {
                responseData.setErrorCode(3);
                responseData.setStatus("登录成功");
                responseData.setObject(loginUser);
            }
        }
        return responseData;
    }

    @RequestMapping("getUserInfo")
    public MyResponseData getUserInfo(HttpSession session){
        MyResponseData responseData = new MyResponseData();
        UserWithRole userWithRole = (UserWithRole)session.getAttribute("loginUser");
        responseData.setObject(userWithRole);
        return responseData;
    }


    @RequestMapping("checkUserNameExist")
    public MyResponseData checkUserNameExist(String u_name) {
        MyResponseData responseData = new MyResponseData();
        List<User> users = service.checkUserNameExist(u_name);
        if (!users.isEmpty()) {
            responseData.setErrorCode(-1);
            responseData.setStatus("用户名已存在，请重新输入");
        }else if(users.isEmpty()){
            responseData.setErrorCode(0);
        }
        return responseData;
    }

    @RequestMapping("deleteOne")
    public MyResponseData delete(Integer u_id){
        Integer count = service.deleteOne(u_id);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("已删除用户");
        return responseData;
    }

    @RequestMapping("insert")
    public MyResponseData insertUser(User user,HttpSession session) {
        MyResponseData responseData = new MyResponseData();

        if(user.getR_name()==""){
            responseData.setErrorCode(-1);
            responseData.setStatus("用户名不能为空");
        }else if(user.getU_pwd()==""){
            responseData.setErrorCode(-2);
            responseData.setStatus("密码不能为空");
        }else if(user.getU_pwd()!="" && user.getR_name()!=""){
            Integer iu = service.insert(user);
            session.setAttribute("insertUser",iu);
            responseData.setErrorCode(0);
            responseData.setStatus("插入成功");
        }

        return responseData;
    }
    @RequestMapping("update")
    public MyResponseData updateUser(User user,HttpSession session) {
        MyResponseData responseData = new MyResponseData();
        Integer iu = service.update(user);
        session.setAttribute("updatetUser",iu);
        if(iu<=0){
            responseData.setErrorCode(-1);
            responseData.setStatus("更新失败");
        }
        else{
            responseData.setErrorCode(0);
            responseData.setStatus("更新成功");
            responseData.setObject(iu);
        }
        return responseData;
    }

    @RequestMapping("selectById")
    public MyResponseData selectById(Integer id){
        User user = service.selectById(id);
        MyResponseData responseData = new MyResponseData();
        responseData.setErrorCode(0);
        responseData.setStatus("success");
        responseData.setObject(user);
        return responseData;
    }
}
